package com.dirk.happie.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.dirk.happie.R;
import com.dirk.happie.beans.BasketItem;
import com.dirk.happie.utils.Common;

public class BasketListAdapter extends ArrayAdapter<BasketItem> {

	Context context;
	int layoutResourceId;
	public Dialog dialog;

	public BasketListAdapter(Context context, int layoutResourceId,
			ArrayList<BasketItem> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View row = convertView;
		ProductTypeHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new ProductTypeHolder();
			holder.txtCount = (TextView) row.findViewById(R.id.txtCount);
			holder.txtName = (TextView) row.findViewById(R.id.txtTitle);
			holder.txtSize = (TextView) row.findViewById(R.id.txtSize);
			holder.price = (TextView) row.findViewById(R.id.txtPrice);
			holder.trash = (ImageView) row.findViewById(R.id.imgTrash);
			holder.pen = (ImageView) row.findViewById(R.id.imgMarker);

			row.setTag(holder);
		} else {
			holder = (ProductTypeHolder) row.getTag();
		}

		final BasketItem product = this.getItem(position);
		holder.txtCount.setText(product.getCount());
		holder.txtCount.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showCountDialog(product);
			}
		});
		holder.pen.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showCountDialog(product);
			}
		});
		holder.txtName.setText(product.getName());
		holder.txtSize.setText(product.getSize());
		holder.price.setText(getString(R.string.currency) + " " + Common.getPrice(product.getPrice()));
		holder.trash.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				remove(getItem(position));
				notifyDataSetChanged();
			}
		});

		return row;
	}
	
	private void showCountDialog(final BasketItem product){
		
		if (dialog != null && dialog.isShowing())
			return;
		dialog = new Dialog(context);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_picker);
		dialog.setCancelable(true);

		final EditText etCount = (EditText) dialog
				.findViewById(R.id.dialog_et);

		Button okButton = (Button) dialog.findViewById(R.id.ok_button);
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!etCount.getText().toString().equals("")) {
					try {
						product.setCount(getParseableCount(etCount
								.getText().toString()));
						notifyDataSetChanged();
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				dialog.cancel();
			}
		});

		dialog.show();
	}
	
	private String getParseableCount(String source){
		int k = source.indexOf(".");
		String retString = source;
		if(k!=-1){
			retString = retString.substring(0, k);
		}
		return retString;
		
	}

	private String getString(int resId) {
		// TODO Auto-generated method stub
		return context.getString(resId);
	}

	static class ProductTypeHolder {
		TextView txtCount;
		TextView txtName;
		TextView txtSize;
		TextView price;
		ImageView trash;
		ImageView pen;

	}
}
